<?php
    /**
    * Users.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    require_once 'Validate.php';
    
    /**
    * The AT_Users class represents the users table in the database.
    * @package Atman
    */
    class AT_Users extends AT_DB_Table
    {
        /**
        * Constructor sets up the table name.
        */
        public function __construct()
        {
            parent::__construct('users');
        }
        
        /**
        * Attempts to retrieve a user's userID by their email. If the email is
        * not found in the database, 0 is returned.
        * @param string $email The email to lookup.
        * @return int
        */
        public function getUserIDByEmail($email)
        {
            if ( !Validate::email($email) )
            {
                return 0;
            }
            
            $sql = 'SELECT id
                        FROM '.$this->tableName.'
                        WHERE email = '.$this->mdb2->quote($email, 'text');
            $result = $this->mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL SELECT failed: '.$result->getMessage();
                $this->reportError($msg, PEAR_LOG_ALERT);
            }
            
            if ( $result->numRows() == 1 )
            {
                $row = $result->fetchRow();
                return $row['id'];
            }
            else
            {
                return 0;
            }
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
